﻿using Unity.Entities;
using UnityEngine;
using Unity.Burst;
using Unity.Mathematics;

[DisableAutoCreation]
public partial class InputSystem : SystemBase
{
    private InputControl _control;

    protected override void OnCreate()
    {
        //实例化 (单例)
        if(!SystemAPI.TryGetSingleton(out InputComp input))
        {
            EntityManager.CreateEntity(typeof(InputComp));
        }
        _control = new InputControl();
        _control.Enable();
    }
    
    protected override void OnUpdate()
    {
        var inputEntity = SystemAPI.GetSingletonEntity<InputComp>();
        SystemAPI.SetComponent(inputEntity, new InputComp
        {
            Dir = new float2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")),
            MousePos = new float2(_control.Player.MousePos.ReadValue<Vector2>()),
            isShoot = _control.Player.Shooot.IsPressed()
        });
    }
}
